//
//  GuestNineModeView.m
//  X-Touch2
//
//  Created by shengchao yang on 12-3-15.
//  Copyright (c) 2012年 foxhis. All rights reserved.
//

#import "GuestNineModeView.h"
#import "Singleton.h"
#import "GridViewItemGuest.h"
#import "TreeTableViewCell.h"
#import "Common.h"
#import "FileManager.h"

@interface GuestNineModeView(Private)
-(void) clickOnSectionIndex:(NSInteger)sectionIndex;
-(void) clickOnCellIndex:(NSInteger)cellIndex;
@end

@implementation GuestNineModeView
- (id)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        
    }
    return self;
}
#pragma mark - 自定义函数
- (IBAction)buttonClicked:(id)sender {
    if (AppSession.isAnimating) {
        MyNSLog(@"上一个动画还未完成...");
        return;
    }
    [AppSession playMusic:VoiceFile(@"clicked.caf")];
    NSInteger bid = [sender tag];
//    UIButton *btn = (UIButton *)[self viewWithTag:bid];
    if (bid == TagOfUIButton) {
        MyNSLog(@"切换到推荐菜界面...");
        postN(SysMsg_GuestOrdering_removeNineModeIntoCommend);
    }
}
//点击大类调用函数
-(void) clickOnSectionIndex:(NSInteger)sectionIndex{
    [AppSession initPluSortByMenuIndex:sectionIndex];
//    [AppSession initPlusByMenuIndex:sectionIndex];//初始化大类下面的菜品数组
    [self clickOnCellIndex:0];
}
//点击小类调用函数
-(void) clickOnCellIndex:(NSInteger)cellIndex{
    [AppSession initPlusBySortIndex:cellIndex];
    [gridView setCurrentPageIndex:0];//点击小类后，默认显示第一页
}

#pragma mark - UIGridViewDataSource
-(NSInteger) gridViewRows:(UIGridView *)gridView{
    return 3;
}
-(NSInteger) gridViewCols:(UIGridView *)gridView{
    return 3;
}
-(NSInteger) gridViewTotalCount:(UIGridView *)gridView{
    return [AppSession.orderingPluPlus count];
}
-(GridViewItem *) gridView:(UIGridView *)gridView1 itemAtIndex:(NSInteger)index{
    NSString *CellIdentifier = @"CellIdentifier1";
    GridViewItemGuest *item = (GridViewItemGuest *)[gridView1 dequeueReusableCellWithIdentifier:CellIdentifier];
    if (item == nil){
        item = [[GridViewItemGuest alloc] init];
    }
    PluPlu *tempPlu = (PluPlu *)[AppSession.orderingPluPlus objectAtIndex:index];
    [item setData:tempPlu];
    return item;
}
-(BOOL) gridViewPageControlEnable:(UIGridView *)gridView{  //是否使用pageControl控件
    return YES;
}
-(BOOL) gridViewDirection:(UIGridView *)gridView{          //滑动的方向(YES-横向默认  NO-纵向)
    return YES;
}
-(BOOL) gridViewShowScrollIndicator:(UIGridView *)gridView{//是否显示滑动条Indicator(默认NO)
    return NO;
}
#pragma mark - UIGridViewDelegate
-(void) gridView:(UIGridView *)gridView didSelectItemAtIndex:(NSInteger)index{
    MyNSLog(@"弹出全屏窗口 index=%d", index);
    AppSession.pluIndex = index;
    AppSession.fullScreenType = 1;//设置显示菜品数组内容
    postN(SysMsg_RootView_PopOutPluPlu);//弹出大图模式
}

#pragma mark - UITableView datasource
-(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView{
    return [AppSession.pluMenus count];
}
-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{    
    if (section == AppSession.menuIndex) {
        return [AppSession.pluSorts count];
    }else {
        return 0;
    }
}
//自定义row
-(UITableViewCell*) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *CellIdentifier = @"CellIdentifier";
	TreeTableViewCell *cell = (TreeTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
	if (cell == nil){
		cell = [[[TreeTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
	}    
    cell.selectionStyle=UITableViewCellSelectionStyleNone;//设置选择cell时，没有任何背景颜色
    cell.tag = TagOfUITableViewCell + indexPath.row;
    PluSort *tempSort = (PluSort *)[AppSession.pluSorts objectAtIndex:indexPath.row];
    [cell setData:JsonField(tempSort.psDescripts) sortCode:tempSort.psCode];
    [cell setSelectedCell:(AppSession.sortIndex == indexPath.row)];//设置选中状态    
    return cell;
}
//自定义section
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    TreeTableViewSection *sectionView = [[TreeTableViewSection alloc] initWithNibName:@"TreeTableViewSection" bundle:nil];
    sectionView.view.tag = TagOfUITableViewSection + section;
    sectionView.delegate = self;
    PluMenu *tempMenu = (PluMenu *)[AppSession.pluMenus objectAtIndex:section];
    [sectionView setData:JsonField(tempMenu.pmDescripts) menuCode:tempMenu.pmCode];
    [sectionView setSelectedSection:(AppSession.menuIndex == section)];
	return sectionView.view;
}

#pragma mark - datadelegate
//点击大类名称时触发
-(void) tableViewSection:(UIView *)sectionView didTappedAtSection:(NSInteger)section{
    MyNSLog(@"click section = %d", section);
    if (AppSession.isAnimating) {
        MyNSLog(@"上一个动画还未完成...");
        return;
    }
    [AppSession playMusic:VoiceFile(@"clicked.caf")];
    if (AppSession.menuIndex == section) {
        if ([AppSession.pluSorts count] == 0) {//全部都收起来了，下面要展开
            MyNSLog(@"展开大类");
            [AppSession initPluSortByMenuIndex:section];//只初始化小类数组
            [tableViewTree reloadData];//只刷新TableView         
        }else {//已经处于展开状态，下面要收缩
            MyNSLog(@"收缩大类");
            [AppSession.pluSorts removeAllObjects];//清空小类数组
            AppSession.menuIndex = -1;//表示没有选择任何大类
            [tableViewTree reloadData];//只刷新TableView
        }
    }else {
        MyNSLog(@"重新初始化大类...");
        [self clickOnSectionIndex:section];
        [tableViewTree reloadData];
        [gridView reloadData];
    }
}
//点击小类名称触发
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    MyNSLog(@"click row = %d", indexPath.row);
    if (AppSession.isAnimating) {
        MyNSLog(@"上一个动画还未完成...");
        return;
    }
    [AppSession playMusic:VoiceFile(@"clicked.caf")];
    if (AppSession.sortIndex == indexPath.row) {
        MyNSLog(@"没有改变小类");
    }else {
        MyNSLog(@"改变了小类...");
        [self clickOnCellIndex:indexPath.row];
        [tableViewTree reloadData];
        [gridView reloadData];
    }
}
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
    NSIndexPath *lastIndexPath = [[tableView indexPathsForVisibleRows] lastObject];
    if (lastIndexPath.row < ([AppSession.pluSorts count]-1)) {//设置向下的箭头是否显示
        [self viewWithTag:TagOfUIImageView].hidden = NO;
    }
    else{
        [self viewWithTag:TagOfUIImageView].hidden = YES;
    }
}

//刷新界面
-(void) refreshGuestNineModeView{
    [tableViewTree reloadData];
    [gridView reloadData];    
    //刷新我的点单总数
    postN(@"SysMsg_GuestOrdering_refreshToolBar");
}

// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect{
    addNObserver(@selector(refreshGuestNineModeView), @"SysMsg_GuestNineModeView_refreshGuestNineModeView");
    
    tableViewTree = (UITableView *)[self viewWithTag:TagOfUITableView];
    [tableViewTree setBackgroundColor:[UIColor clearColor]];
    isNeedRefreshPluSort = YES;
    
    ////////////////////初始化数据 begin////////////////////
    [self clickOnSectionIndex:0];
    [tableViewTree reloadData];
    //刷新我的点单总数
    postN(@"SysMsg_GuestOrdering_refreshToolBar");
    ////////////////////初始化数据 end//////////////////////
    
    [self viewWithTag:TagOfUIImageView].hidden=YES;//向下的箭头默认隐藏
    
    MyNSLog(@"in GuestNineModeView drawRect");
    [self setFrame:CGRectMake(0, 53, 1024, 715)];
    //用自定义的九宫格view替换占位用的普通view，同时移除普通view
    UIView *tempView = (UIView *)[self viewWithTag:TagOfUIView];
    CGRect frame = tempView.frame;
    [tempView removeFromSuperview];
    
    gridView = [[UIGridView alloc] initWithTarget:self withFrame:frame withTag:TagOfUIGridView];
    [self addSubview:gridView];
}

-(void)dealloc{
    MyNSLog(@"GuestNineModeView is deallocing...");
    removeNObserver(@"SysMsg_GuestNineModeView_refreshGuestNineModeView");
    [gridView release];
    gridView = nil;
    [super dealloc];
}

@end
